<script>
import { loginInterceptor } from '@/utils/auth'
import { getUserInfo } from '@/api/user'
export default {
	onLaunch: function() {
		
		// 全局路由拦截
		this.setupRouteInterceptors()
		
		// 初始页面检查
		this.checkInitialPage()
	},
	onShow: function() {
		//console.log('App Show')
		// 检查用户是否已登录
		const token = uni.getStorageSync('token')
		if (token) {
			// 已登录，校验用户状态
			this.checkUserStatus()
		}
	},
	onHide: function() {
//		console.log('App Hide')
	},
	globalData: {
		test: ''
	},
	methods: {
		// 校验用户状态
		async checkUserStatus() {
			try {
				// 调用getUserInfo接口校验用户状态
				const response = await getUserInfo()
				//console.log('用户状态校验成功', response.data)
				// 更新用户信息
				this.$store.commit('user/setUserInfo', response.data)
			} catch (error) {
				//console.error('用户状态校验失败', error)
				// 清除登录状态
				this.$store.commit('user/clearToken')
				// 跳转到登录页面
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		},
		setupRouteInterceptors() {
			const interceptors = ['navigateTo', 'redirectTo', 'switchTab', 'reLaunch']
			
			interceptors.forEach(interceptor => {
			uni.addInterceptor(interceptor, {
				invoke(args) {
				console.log("路由拦截:", interceptor, args.url)
				return loginInterceptor(args.url)
				},
				fail(err) {
				console.log(`${interceptor} fail:`, err)
				}
			})
			})
		},
		checkInitialPage() {
			// 获取当前页面路径
			const pages = getCurrentPages()
			if (pages.length === 0) {
			// 如果是首次加载
			const initialPage =  '/pages/index/index';
			//console.log('初始页面:', initialPage)
			
			// 手动检查初始页面是否需要登录
			if (!loginInterceptor(initialPage)) {
				// 如果需要登录且未登录，则跳转到登录页
				uni.redirectTo({
				url: '/pages/login/login?redirect=' + encodeURIComponent(initialPage)
				})
			}
			}
		}
	}
}
</script>

<style lang="scss">
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-PLUS-NVUE */
	/* uni.css - 通用组件、模板样式库，可以当作一套ui库应用 */
	@import './common/uni.css';
	@import '@/static/customicons.css';
	/* H5 兼容 pc 所需 */
	/* #ifdef H5 */
	@media screen and (min-width: 768px) {
		body {
			overflow-y: scroll;
		}
	}

	/* 顶栏通栏样式 */
	/* .uni-top-window {
	    left: 0;
	    right: 0;
	} */

	uni-page-body {
		background-color: #F5F5F5 !important;
		min-height: 100% !important;
		height: auto !important;
	}

	.uni-top-window uni-tabbar .uni-tabbar {
		background-color: #fff !important;
	}

	.uni-app--showleftwindow .hideOnPc {
		display: none !important;
	}

	/* #endif */

	/* 以下样式用于 hello uni-app 演示所需 */
	page {
		background-color: #efeff4;
		height: 100%;
		font-size: 28rpx;
		/* line-height: 1.8; */
	}

	.fix-pc-padding {
		padding: 0 50px;
	}

	.uni-header-logo {
		padding: 30rpx;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 10rpx;
	}

	.uni-header-image {
		width: 100px;
		height: 100px;
	}

	.uni-hello-text {
		color: #7A7E83;
	}

	.uni-hello-addfile {
		text-align: center;
		line-height: 300rpx;
		background: #FFF;
		padding: 50rpx;
		margin-top: 10px;
		font-size: 38rpx;
		color: #808080;
	}

	/* #endif*/
</style>
